package com.itheima;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManagementSystem {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student(1, "张三", 20, "四川省广元市剑阁县"));
        list.add(new Student(2, "李四", 21, "四川省宜宾市翠屏区"));
        list.add(new Student(3, "王五", 19, "四川省乐山市犍为县"));
        list.add(new Student(4, "赵六", 18, "四川省广安市岳池县"));
        list.add(new Student(5, "孙七", 20, "四川省成都市武侯区"));

        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生");
            System.out.println("4. 查询所有学生");
            System.out.println("5. 查询单个学生");
            System.out.println("6. 退出");
            System.out.println("请输入您的选择：");

            String choice = scanner.next();
            switch (choice) {
                case "1":
                    System.out.println("添加学生");

                    int id;
                    boolean idExists;
                    do {
                        System.out.println("请输入学生id：");
                        id = scanner.nextInt();

                        idExists = false;
                        for (Student s : list) {
                            if (s.getId() == id) {
                                System.out.println("学生id已存在，请重新输入");
                                idExists = true;
                                break;
                            }
                        }
                    } while (idExists);

                    System.out.println("请输入学生姓名：");
                    String name = scanner.next();

                    System.out.println("请输入学生年龄：");
                    int age = scanner.nextInt();

                    System.out.println("请输入学生地址：");
                    String address = scanner.next();

                    Student student = new Student(id, name, age, address);
                    list.add(student);
                    System.out.println("添加成功");
                    break;

                case "2":
                    System.out.println("删除学生");
                    System.out.println("请输入学生id：");
                    int id1 = scanner.nextInt();
                    boolean found = false;
                    for (int i = 0; i < list.size(); i++) {
                        Student s = list.get(i);
                        if (s.getId() == id1) {
                            list.remove(i);
                            System.out.println("删除成功");
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        System.out.println("没有此id");
                        System.out.println("删除失败");
                    }
                    break;

                case "3":
                    System.out.println("修改学生");
                    System.out.println("请输入学生id：");
                    int id2 = scanner.nextInt();
                    boolean modified = false;
                    for (int i = 0; i < list.size(); i++) {
                        Student s = list.get(i);
                        if (s.getId() == id2) {
                            System.out.println("请输入学生姓名：");
                            String newName = scanner.next();
                            System.out.println("请输入学生年龄：");
                            int newAge = scanner.nextInt();
                            System.out.println("请输入学生地址：");
                            String newAddress = scanner.next();
                            list.set(i, new Student(id2, newName, newAge, newAddress));
                            System.out.println("修改成功");
                            modified = true;
                            break;
                        }
                    }
                    if (!modified) {
                        System.out.println("没有此id");
                        System.out.println("修改失败");
                    }
                    break;

                case "4":
                    System.out.println("查询所有学生");
                    printAllStudents(list);
                    break;

                case "5":
                    System.out.println("查询单个学生");
                    System.out.println("请输入学生id：");
                    int id3 = scanner.nextInt();
                    boolean exists = false;
                    for (Student s : list) {
                        if (s.getId() == id3) {
                            System.out.println(s);
                            exists = true;
                            break;
                        }
                    }
                    if (!exists) {
                        System.out.println("没有此id");
                        System.out.println("查询失败");
                    }
                    break;

                case "6":
                    System.out.println("退出");
                    scanner.close();
                    System.out.println("感谢您的使用，再见");
                    System.exit(0);
                    break;

                default:
                    System.out.println("输入错误");
                    break;
            }
        }
    }

    private static void printAllStudents(ArrayList<Student> list) {
        System.out.println("学号\t姓名\t年龄\t地址");
        for (Student s : list) {
            System.out.printf("%d\t%s\t%s\t%s%n",
                    s.getId(), s.getName(), s.getAgeWithUnit(), s.getAddress());
        }
    }
}